//
//  FloatInfoViewController.m
//  Utils
//
//  Created by enghou on 2018/4/26.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "FloatInfoViewController.h"
#import "NSMutableDictionary+SetContent.h"
#import "PopulationContainerViewController.h"
@interface FloatInfoViewController ()

@property(nonatomic,strong)UIDatePicker *datePicker;


@end

@implementation FloatInfoViewController

-(NSMutableDictionary *)param{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    BOOL result = [param setContent:self.locateModel.ids forKey:@"population.stayLocationType.id" conentNeeded:YES];
    if (!result) {
        [self alertSome:@"请输入住所类型!"];
        return nil;
    }
    result = [param setContent:self.reasonModel.ids forKey:@"population.inflowingReason.id" conentNeeded:NO];
    result = [param setContent:self.cardTypeModel.ids forKey:@"population.registrationType.id" conentNeeded:NO];
    [param setContent:self.cardNumberField.text forKey:@"population.certificateNumber" conentNeeded:NO];
    [param setContent:self.registDateField.text forKey:@"population.registerDate" conentNeeded:NO];
    [param setContent:self.endDateField.text forKey:@"population.expectedDatedue" conentNeeded:NO];
    if (self.yesBtn.isSelected) {
        [param setContent:@"1" forKey:@"population.isEmphasis" conentNeeded:NO];
    }else{
        [param setContent:@"0" forKey:@"population.isEmphasis" conentNeeded:NO];
    }
    return param;
}

-(void)query{
    NSDictionary *param = [self param];
    if ([self.theDelete respondsToSelector:@selector(fetchModel:)]) {
        [self.theDelete fetchModel:param];
        [self pop];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"查询" style:UIBarButtonItemStylePlain target:self action:@selector(query)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 240)];
    self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    self.datePicker.backgroundColor = [UIColor whiteColor];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePicker.date = [NSDate date];
    [self configInputField:self.registDateField];
    [self configInputField:self.endDateField];
    
    self.locateModel = [[PropertyDictItemModel alloc]init];
    self.reasonModel = [[PropertyDictItemModel alloc]init];
    self.cardTypeModel = [[PropertyDictItemModel alloc]init];
}

-(void)sureAction{
    NSString *date = [[self.datePicker date]dateLineStrOnlyDay];
    if ([self.registDateField isFirstResponder]) {
        self.registDateField.text = date;
    }else if ([self.endDateField isFirstResponder]){
        self.endDateField.text = date;
    }
    [self.view endEditing:YES];
}

-(void)chooseCardType{
    if (!self.cardTypes) {
        [self commonAlertWithContent:@"办证类型" choosenBlock:^(PropertyDictItemModel *model) {
            self.cardTypeModel = model;
            self.cardTypeField.text = self.cardTypeModel.displayName;
        } getList:^(NSArray *array) {
            self.cardTypes = array;
        }];
    }else{
        [self alertWithArray:self.relations choosenBock:^(PropertyDictItemModel *model) {
            self.cardTypeModel = model;
            self.cardTypeField.text = self.cardTypeModel.displayName;
        }];
    }
}

-(void)chooseReasons{
    if (!self.reasons) {
        [self commonAlertWithContent:@"流入原因" choosenBlock:^(PropertyDictItemModel *model) {
            self.reasonModel = model;
            self.intoReasonField.text = self.reasonModel.displayName;
        } getList:^(NSArray *array) {
            self.reasons = array;
        }];
    }else{
        [self alertWithArray:self.relations choosenBock:^(PropertyDictItemModel *model) {
            self.reasonModel = model;
            self.intoReasonField.text = self.reasonModel.displayName;
        }];
    }
}

-(void)chooseLocateTypes{
    if (!self.locateTypes) {
        [self commonAlertWithContent:@"流动人口_住所类型" choosenBlock:^(PropertyDictItemModel *model) {
            self.locateModel = model;
            self.locateTypeField.text = self.locateModel.displayName;
        } getList:^(NSArray *array) {
            self.locateTypes = array;
        }];
    }else{
        [self alertWithArray:self.relations choosenBock:^(PropertyDictItemModel *model) {
            self.locateModel = model;
            self.locateTypeField.text = self.locateModel.displayName;
        }];
    }
}

-(void)cancelAction{
    [self.view endEditing:YES];
}

-(void)configInputField:(UITextField *)field{
    field.inputView = self.datePicker;
    field.inputAccessoryView = [self inputAccessView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)alertSomes:(NSString *)msg{
    PopulationContainerViewController *container = (PopulationContainerViewController *)self.theDelete;
    [container turnToIndex:[container.ctlArray indexOfObject:self] andAlert:msg];
}

#pragma mark - Table view data source
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [self chooseLocateTypes];
    }else if (indexPath.row == 1){
        [self chooseReasons];
    }else if (indexPath.row == 2){
        [self chooseCardType];
    }
}

- (IBAction)noAction:(id)sender {
    self.noBtn.selected = YES;
    self.yesBtn.selected = NO;
}

- (IBAction)yesAction:(id)sender {
    self.noBtn.selected = NO;
    self.yesBtn.selected = YES;
}

- (IBAction)chooseRegistDateAction:(id)sender {
    [self.registDateField becomeFirstResponder];
}

- (IBAction)chooseEndDateAction:(id)sender {
    [self.endDateField becomeFirstResponder];
}
@end
